import java.sql.*;

public class MySQLConnectionDemo {

    public static void main(String[] args) {

        // 数据库连接信息
        String url = "jdbc:mysql://localhost:3306/mysql";
        String username = "root";
        String password = "DAxiang123!_";


        try {
            // 加载 MySQL JDBC 驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 建立数据库连接
            Connection connection = DriverManager.getConnection(url, username, password);
            // 创建 Statement 对象
            Statement statement = connection.createStatement();
            // 执行 SQL 查询
            String sql = "SELECT * FROM user";
            ResultSet resultSet = statement.executeQuery(sql);
            // 处理查询结果
            while (resultSet.next()) {
                String name = resultSet.getString("User");
                System.out.println("Name: " + name);
            }
            // 关闭资源
            resultSet.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
